package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.GoodsBo;
import com.cskaoyan.catmall.model.bo.GoodsDetailBo;
import com.cskaoyan.catmall.model.pojo.MarketGoods;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.CatAndBrandVo;
import com.cskaoyan.catmall.model.vo.GoodsDetailVo;
import com.cskaoyan.catmall.model.vo.GoodsVo;
import com.cskaoyan.catmall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品模块
 * 创建日期: 2022/01/06 23:40
 *
 * @author ghr
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     * @param goodsBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 商品列表
     * @author ghr
     * @date 2022/1/9 4:14 下午
     */
    @GetMapping("list")
    public BaseRespVo selectGoodsList(@Validated GoodsBo goodsBo, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return BaseRespVo.fail("参数错误");
        }
        GoodsVo goodsVo = goodsService.selectGoodsList(goodsBo);
        return BaseRespVo.ok(goodsVo);
    }

    /**
     * @param
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 品牌和类目
     * @author ghr
     * @date 2022/1/8 2:39 下午
     */
    @GetMapping("catAndBrand")
    public BaseRespVo selectCatAndBrand() {
        CatAndBrandVo catAndBrandVo = goodsService.selectCatAndBrand();
        return BaseRespVo.ok(catAndBrandVo);
    }

    /**
     * @param goodsDetailBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 商品上架
     * @author ghr
     * @date 2022/1/9 3:26 下午
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody GoodsDetailBo goodsDetailBo) {
        if (StringUtils.isEmpty(goodsDetailBo.getGoods().getGoodsSn()) || StringUtils.isEmpty(goodsDetailBo.getGoods().getName())) {
            return BaseRespVo.fail("参数不能为空");
        }
        MarketGoods goods = goodsDetailBo.getGoods();
        String goodsSn = goods.getGoodsSn();
        String name = goods.getName();
        int code = goodsService.selectDuplicateGoods(goodsSn, name);
        if (code > 0) {
            return BaseRespVo.fail("商品重复");
        }
        goodsService.insertGoodsDetail(goodsDetailBo);

        return BaseRespVo.ok();
    }

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 商品细节
     * @author ghr
     * @date 2022/1/9 3:59 下午
     */
    @GetMapping("detail")
    public BaseRespVo selectGoodsDetail(Integer id) {
        GoodsDetailVo goodsDetailVo = goodsService.selectGoodsDetail(id);
        return BaseRespVo.ok(goodsDetailVo);
    }

    /**
     * @param
     * @param goodsDetailBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 编辑商品
     * @author ghr
     * @date 2022/1/9 4:23 下午
     */
    @PostMapping("update")
    public BaseRespVo updateGoods(@RequestBody GoodsDetailBo goodsDetailBo) {
        MarketGoods goods = goodsDetailBo.getGoods();
        String name = goods.getName();
        if (StringUtils.isEmpty(name)) {
            return BaseRespVo.fail("商品名不能为空");
        }
        int code = goodsService.selectDuplicateGoodsByName(name);
        if (code > 0) {
            return BaseRespVo.fail("商品名已经存在");
        }
        goodsService.updateGoods(goodsDetailBo);
        return BaseRespVo.ok();
    }

    /**
     * @param
     * @param marketGoods
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 删除商品
     * @author ghr
     * @date 2022/1/9 4:01 下午
     */
    @PostMapping("delete")
    public BaseRespVo deleteGoods(@RequestBody MarketGoods marketGoods) {
        goodsService.deleteGoods(marketGoods);
        return BaseRespVo.ok();
    }
}